package top.yxqz.springmvc.file.controller;

import com.qiniu.storage.DownloadUrl;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;

/**
 * @author: 余小小
 * @date: 2023/5/11
 * @description：七牛云
 */
@Controller
@RequestMapping("/qn")
public class QNController {
    @GetMapping("/down")
    public ResponseEntity<byte[]> testResponseEntity(HttpSession session) throws IOException {
        String fileName = "/soft6.jpg";
        String domainOfBucket = "http://rugyk26bd.hn-bkt.clouddn.com";
        String encodedFileName = URLEncoder.encode(fileName, "utf-8").replace("+", "%20");
//        String finalUrl = String.format("%s/%s", domainOfBucket, encodedFileName);
        String finalUrl = domainOfBucket+fileName;
        //创建输入流
//        InputStream is = new FileInputStream(finalUrl);
        //创建字节数组
        byte[] bytes = new byte[127];
        //将流读到字节数组中
//        is.read(bytes);
        //创建HttpHeaders对象设置响应头信息
        MultiValueMap<String, String> headers = new HttpHeaders();
        //设置要下载方式以及下载文件的名字
        headers.add("Content-Disposition", "attachment;filename=1.jpg");
        //设置响应状态码
        HttpStatus statusCode = HttpStatus.OK;
        //创建ResponseEntity对象
        ResponseEntity<byte[]> responseEntity = new ResponseEntity<>(bytes, headers, statusCode);
        System.out.println("七牛云下载");
// domain   下载 domain, eg: qiniu.com【必须】
// useHttps 是否使用 https【必须】
// key      下载资源在七牛云存储的 key【必须】
        DownloadUrl url = new DownloadUrl("rugyk26bd.hn-bkt.clouddn.com", true, "yydsroom");
//        url.setAttname(attname) // 配置 attname
//                .setFop(fop) // 配置 fop
//                .setStyle(style, styleSeparator, styleParam) // 配置 style
        String urlString = url.buildURL();
        System.out.println(urlString);

        return responseEntity;
    }
}
